package edu.stu.book.book.keeping.ui.screen.home

import android.annotation.SuppressLint
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.BottomAppBar
import androidx.compose.material.BottomNavigationItem
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import edu.stu.book.book.keeping.ui.nav.ScreenPage
import edu.stu.book.book.keeping.utils.ColorUtils

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun MainScreen() {

    val items = listOf(
        ScreenPage.Index,
        ScreenPage.Chart,
        ScreenPage.Add,
        ScreenPage.Find,
        ScreenPage.User
    )

    val navController = rememberNavController()
    val context = LocalContext.current

    Scaffold(
        bottomBar = {
            BottomAppBar(
                elevation = 0.dp,
                backgroundColor = Color.Transparent,
                contentColor = Color.Transparent,
                modifier = Modifier
                    .wrapContentHeight()
                    .fillMaxWidth()
            ) {
                val navBackStackEntry by navController.currentBackStackEntryAsState()
                val currentDestination = navBackStackEntry?.destination
                var isSelected: Boolean
                items.forEach { screenPage ->
                    isSelected =
                        currentDestination?.hierarchy?.any { it.route == screenPage.route } == true
                    BottomNavigationItem(
                        selected = isSelected,
                        onClick = {
                            navController.navigate(screenPage.route) {
                                //点击Item时，清空栈内到NavOptionsBuilder.popUpTo ID之间的所有Item

                                // 避免栈内节点的持续增加，同时saveState用于界面状态的恢复
                                popUpTo(navController.graph.findStartDestination().id) {
                                    saveState = true
                                }

                                // 避免多次点击Item时产生多个实列
                                launchSingleTop = true
                                // 当再次点击之前的Item时，恢复状态
                                restoreState = true
                            }
                        }, icon = {
                            Icon(
                                modifier = Modifier.size(24.dp),
                                imageVector = screenPage.icon,
                                contentDescription = context.getString(screenPage.resId),
                                tint = ColorUtils.getColor(context),
                            )
                        },
                        alwaysShowLabel = screenPage.isShowText,
                        label = {
                            AnimatedVisibility(isSelected) {
                                androidx.compose.material3.Text(
                                    text = context.getString(screenPage.resId),
                                    color = ColorUtils.getColor(context),
                                    fontSize = 14.sp,
                                )
                            }
                        }, modifier = if (screenPage.isShowText) {
                            Modifier.padding(top = 10.dp)
                        } else {
                            Modifier.padding(top = 0.dp)
                        }
                    )
                }
            }
        }
    ) {
        NavHost(navController = navController, startDestination = ScreenPage.Index.route) {
            composable(ScreenPage.Index.route) {
                IndexScreen(navController)
            }
            composable(ScreenPage.Chart.route) {
                ChartScreen(navController)
            }
            composable(ScreenPage.Add.route) {
                AddScreen(navController)
            }
            composable(ScreenPage.Find.route) {
                FindScreen(navController)
            }
            composable(ScreenPage.User.route) {
                UserScreen(navController)
            }
        }
    }
}
